﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FreeOnlineApplication.Class.Entity;
using MySql.Data.MySqlClient;
using System.Configuration;
using System.Collections;

namespace FreeOnlineApplication.Class.Repository
{
    public class SupplierRepository
    {
        protected MySqlConnection m_connection;
        MySqlCommand aCommand = new MySqlCommand();

        public SupplierRepository(MySqlConnection connection)
        {
            m_connection = connection;
            aCommand.Connection = m_connection;
        }
        public void Insert(Supplier cust)
        {
            string insertSQL = String.Format(@"insert into table_supplier 
                (
                    supplier_code,
                    supplier_idno,
                    supplier_name,
                    supplier_address,
                    supplier_email,
                    supplier_phone,
                    supplier_job,
                    supplier_gender,
                    supplier_dob,
                    unit_id) 
                VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}',{9})",
                cust.CODE, cust.ID_NO, cust.NAME, cust.ADDRESS,
                cust.EMAIL, cust.PHONE, cust.JOB, cust.GENDER, cust.DOB.ToString(Tools.DATE_FORMAT),
                cust.UNIT.ID);
            aCommand.CommandText = insertSQL;
            aCommand.ExecuteNonQuery();
            cust.ID = GetMaximumId();
        }
        public Supplier GetById(int id)
        {
            string getByID = String.Format("select * from table_supplier where supplier_id = {0}", id);
            aCommand.CommandText = getByID;
            MySqlDataReader aReader = aCommand.ExecuteReader();
            Supplier cust = transform(aReader);
            aReader.Close();
            return cust;
        }
        public void Update(Supplier cust)
        {
            string updateSQL = String.Format(@"update table_supplier set 
                supplier_code = '{0}', 
                supplier_idno = '{1}', 
                supplier_name='{2}',
                supplier_address='{3}',
                supplier_email='{4}',
                supplier_phone='{5}',
                supplier_job='{6}',
                supplier_gender='{7}',
                supplier_dob='{8}',
                unit_id = {9}
                where supplier_id = {10}",
                cust.CODE, cust.ID_NO, cust.NAME, cust.ADDRESS, cust.EMAIL,
                cust.PHONE, cust.JOB, cust.GENDER,
                cust.DOB.ToString(Tools.DATE_FORMAT),
                cust.UNIT.ID, cust.ID);
            aCommand.CommandText = updateSQL;
            aCommand.ExecuteNonQuery();
        }
        public void Delete(Supplier cust)
        {
            string deleteSQL = "delete from table_supplier where supplier_id = " + cust.ID;
            aCommand.CommandText = deleteSQL;
            aCommand.ExecuteNonQuery();
        }
        public int GetMaximumId()
        {
            string getByID = String.Format("SELECT max(supplier_id) from table_supplier");
            aCommand.CommandText = getByID;
            object r = aCommand.ExecuteScalar();
            int aReader = r is System.DBNull ? 0 : Convert.ToInt32(r);
            return aReader;
        }
        public int GetCount(int unit)
        {
            string getByID = String.Format("SELECT count(*) from table_supplier where unit_id = {0}", unit);
            aCommand.CommandText = getByID;
            object r = aCommand.ExecuteScalar();
            int aReader = r is System.DBNull ? 0 : Convert.ToInt32(r);
            return aReader;
        }
        public IList GetAll(int cust)
        {
            string sql = String.Format("select * from table_supplier where unit_id = {0}", cust);
            aCommand.CommandText = sql;
            MySqlDataReader aReader = aCommand.ExecuteReader();
            IList users = transformList(aReader);
            aReader.Close();
            return users;
        }
        private Supplier transform(MySqlDataReader aReader)
        {
            Supplier cust = null;
            while (aReader.Read())
            {
                cust = new Supplier();
                cust.ID = Convert.ToInt32(aReader["supplier_id"]);
                cust.CODE = aReader["supplier_code"].ToString();
                cust.ID_NO = aReader["supplier_idno"].ToString();
                cust.NAME = aReader["supplier_name"].ToString();
                cust.ADDRESS = aReader["supplier_address"].ToString();
                cust.EMAIL = aReader["supplier_email"].ToString();
                cust.PHONE = aReader["supplier_phone"].ToString();
                cust.JOB = aReader["supplier_job"].ToString();
                cust.GENDER = aReader["supplier_gender"].ToString();
                cust.DOB = Convert.ToDateTime(aReader["supplier_dob"]);
                cust.UNIT = new Unit(Convert.ToInt32(aReader["unit_id"]));
            }
            return cust;
        }
        private IList transformList(MySqlDataReader aReader)
        {
            IList result = new ArrayList();
            while (aReader.Read())
            {
                Supplier cust = new Supplier();
                cust.ID = Convert.ToInt32(aReader["supplier_id"]);
                cust.CODE = aReader["supplier_code"].ToString();
                cust.ID_NO = aReader["supplier_idno"].ToString();
                cust.NAME = aReader["supplier_name"].ToString();
                cust.ADDRESS = aReader["supplier_address"].ToString();
                cust.EMAIL = aReader["supplier_email"].ToString();
                cust.PHONE = aReader["supplier_phone"].ToString();
                cust.JOB = aReader["supplier_job"].ToString();
                cust.GENDER = aReader["supplier_gender"].ToString();
                cust.DOB = Convert.ToDateTime(aReader["supplier_dob"]);
                cust.UNIT = new Unit(Convert.ToInt32(aReader["unit_id"]));
                result.Add(cust);
            }
            return result;
        }
    }
}
